#include <iostream>  
  
using namespace std;  
  
struct ListNode {  
    int val;  
    ListNode *next;  
    ListNode(int x) : val(x), next(NULL) {}  
};  
  
void removeElement(ListNode* &head, int val) {  
    ListNode* current = head;  
    ListNode* prev = nullptr;  
      
    while(current != nullptr && current->val != val) {  
        prev = current;  
        current = current->next;  
    }  
      
    if (current != nullptr) {  
        if (prev != nullptr) {  
            prev->next = current->next;  
        } else {  
            head = current->next;  
        }  
        delete current;  
    }  
}  
  
void printList(ListNode* head) {  
    ListNode* current = head;  
    while(current != nullptr) {  
        cout << current->val << " ";  
        current = current->next;  
    }  
    cout << endl;  
}  
  
int main() {  
    ListNode* head = new ListNode(1);  
    head->next = new ListNode(2);  
    head->next->next = new ListNode(6);  
    head->next->next->next = new ListNode(3);  
    head->next->next->next->next = new ListNode(4);  
    head->next->next->next->next->next = new ListNode(5);  
    head->next->next->next->next->next->next = new ListNode(6);  
    
    cout << "Original list: ";  
    printList(head);  
    
    removeElement(head, 6);  
    
    cout << "List after removing 6: ";  
    printList(head);  
    
    return 0;  
}